package web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Loggin
 */
@WebServlet("/Loggin")
public class Loggin extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private int player1;
    private int player2;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Loggin() {
        super();
        player1 = 0;
        player2 = 0;
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();
		
		HttpSession session = request.getSession();
	 	String username = request.getParameter("un");
		session.setAttribute("UserName", username);
		if(player1 == 0){
			session.setAttribute("ID", 1);
			player1 = 1;
			out.println("You are player 1");
		}
		else{
			session.setAttribute("ID", 2);
			player2 = 2;	
			out.println("You are player 2");
	    }

		    
	}

}
